package com.base.cn.platform.os.service.order.order;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.order.order.OrderBookDao;
import com.base.cn.platform.os.entity.order.order.Order;
import com.base.cn.platform.os.entity.order.order.OrderBook;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 图书类型订单扩展数据
 *
 * @author s.li
 * @create 2018-05-09-18:39
 */
@Service
public class OrderBookBiz extends BaseBiz<OrderBook,OrderBookDao> {

    @Async
    public void createOrderBook(Order order){
        OrderBook orderBook = order.getOrderBook();
        this.save(orderBook);
    }
    /**
     * 通过订单ID串查询图书扩展订单数据
     * @param orderIds 订单ID
     * @return Map<BigDecimal,OrderBook>
     */
    public Map<BigDecimal,OrderBook> findOrderBookByOrderIds(String orderIds){
        List<OrderBook> orderBookList = this.find(" orderId in ("+orderIds+")",null,null);
        if(ObjectUtils.isNotEmpty(orderBookList)){
            return orderBookList.stream().collect(Collectors.toMap(e->e.getOrderId(),e->e));
        }
        return null;
    }

    /**
     * 设置发货
     * @param orderBook
     */
    public void sendOutOrder(OrderBook orderBook){
        orderBook.setMailStatus(2);
        this.updateByWhereSql(orderBook," orderId="+orderBook.getOrderId());
    }

    /**
     * 修改签收状态
     * @param userId 用户ID
     * @param orderId 订单ID
     * @param signStatus 签到状态
     */
    public void updateSignStatus(BigDecimal userId,BigDecimal orderId,int signStatus){
        OrderBook orderBook = new OrderBook();
        orderBook.setSignStatus(signStatus);
        this.updateByWhereSql(orderBook," orderId = "+orderId+" and userId="+userId);
    }
}
